package com.steellee.leeexcel;

import com.steellee.leeexcel.dto.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PaExcelWriteByTemplateDemo extends Parent {
    @Autowired
    private PaExcelWrite operate;

    /**
     * 测试写出文件-输出到指定文件目录
     * 参数: 数据对象类.class, 数据集合dataList, sheetName, 输出文件全路径filePath
     */
    @Test
    public void testriteByTemplate1() throws Exception {
        List<ExampleDtoOneDto> dataList = new ArrayList<>();
        ExampleDtoOneDto dto = new ExampleDtoOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("bb");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");
        dataList.add(dto);
        String filePath = "D:\\Temp\\你好呀" + System.currentTimeMillis() + ".xlsx";
        operate.writeByTemplate(ExampleDtoOneDto.class, dataList, null, getObjTempInputStream(), filePath);
        System.out.println("文件写出成功! ");
    }

    /**
     * ·测试写出文件流
     * 参数: 数据对象类.class, 数据集合dataList, sheetName, 输出文件流OutputStream
     */
    @Test
    public void testWriteByTemplate2() throws Exception {
        List<ExampleDtoOneDto> dataList = new ArrayList<>();
        ExampleDtoOneDto dto = new ExampleDtoOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("bb");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");
        dataList.add(dto);
        String filePath = "D:\\Temp\\你好呀" + System.currentTimeMillis() + ".xlsx";
        OutputStream os = Files.newOutputStream(Paths.get(filePath));
        operate.writeByTemplate(ExampleDtoOneDto.class, dataList, 0, getObjTempInputStream(), os);
        System.out.println("文件写出成功! ");
    }

    /**
     * 测试写出文件-输出到指定文件目录-多个 sheet数据写出
     * 参数: 入参对象集合 List<MetaExcelWriteDTO>, 输出文件全路径filePath
     */
    @Test
    public void testWriteByTemplate3() throws Exception {
        List<ExampleDtoOneDto> dataList = new ArrayList<>();
        ExampleDtoOneDto dto = new ExampleDtoOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("bb");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");

        dataList.add(dto);
        dto = new ExampleDtoOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("bb");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");
        dataList.add(dto);
        List<ExampleDtoTwoDto> data2List = new ArrayList<>();
        ExampleDtoTwoDto dto1 = new ExampleDtoTwoDto();
        dto1.setDocLevel1("11");
        dto1.setDocName1("22");
        dto1.setPlanDesc1("aa");
        dto1.setPlanType1("bb");
        dto1.setPlanStartTime1(new Date());
        dto1.setPlanEndTime1(new Date());
        dto1.setUm1("cc");
        data2List.add(dto1);
        dto1 = new ExampleDtoTwoDto();
        dto1.setDocLevel1("11");
        dto1.setDocName1("22");
        dto1.setPlanDesc1("aa");
        dto1.setPlanType1("bb");
        dto1.setPlanStartTime1(new Date());
        dto1.setPlanEndTime1(new Date());
        dto1.setUm1("cc");
        data2List.add(dto1);
        List<MetaExcelWriteDTO> metas = new ArrayList<>();
        MetaExcelWriteDTO meta = new MetaExcelWriteDTO().setParamClazz(ExampleDtoOneDto.class)
                .setDataList(dataList).setSheetName("123");
        metas.add(meta);
        meta = new MetaExcelWriteDTO().setParamClazz(ExampleDtoTwoDto.class).setDataList(data2List).setSheetName("456");
        metas.add(meta);
        String filePath = "D:\\Temp\\你好呀" + System.currentTimeMillis() + ".xlsx";
        operate.writeByTemplate(metas, getObjTempInputStream(), filePath, false);
        System.out.println("文件写出成功! ");
    }

    /**
     * 测试写出文件流-多个 sheet数据写出
     * 参数: 入参对象集合 List<MetaExcelWriteDTO>, 输出文件流OutputStream
     */
    @Test
    public void testWriteByTemplate4() throws Exception {
        List<ExampleDtoOneDto> dataList = new ArrayList<>();
        ExampleDtoOneDto dto = new ExampleDtoOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("bb");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");
        dataList.add(dto);
        dto = new ExampleDtoOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("bb");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");
        dataList.add(dto);
        List<ExampleDtoTwoDto> data2List = new ArrayList<>();
        ExampleDtoTwoDto dto1 = new ExampleDtoTwoDto();
        dto1.setDocLevel1("11");
        dto1.setDocName1("22");
        dto1.setPlanDesc1("aa");
        dto1.setPlanType1("bb");
        dto1.setPlanStartTime1(new Date());
        dto1.setPlanEndTime1(new Date());
        dto1.setUm1("cc");
        data2List.add(dto1);
        dto1 = new ExampleDtoTwoDto();

        dto1.setDocLevel1("11");
        dto1.setDocName1("22");
        dto1.setPlanDesc1("aa");
        dto1.setPlanType1("bb");
        dto1.setPlanStartTime1(new Date());
        dto1.setPlanEndTime1(new Date());
        dto1.setUm1("cc");
        data2List.add(dto1);
        List<MetaExcelWriteDTO> metas = new ArrayList<>();
        MetaExcelWriteDTO meta = new MetaExcelWriteDTO().setParamClazz(ExampleDtoOneDto.class).setDataList(dataList);
        metas.add(meta);
        meta = new MetaExcelWriteDTO().setParamClazz(ExampleDtoTwoDto.class).setDataList(data2List);
        metas.add(meta);
        String filePath = "D:\\Temp\\你好呀" + System.currentTimeMillis() + ".xlsx";
        OutputStream os = Files.newOutputStream(Paths.get(filePath));
        operate.writeByTemplate(metas, getObjTempInputStream(), os, false);
        System.out.println("文件写出成功! ");
    }

    /**
     * 测试 Map数据写出文件流-多个 sheet数据写出
     * 参数: 入参对象集合 List<MetaExcelWriteDTO>, 输出文件流OutputStream
     */
    @Test
    public void testWriteByTemplate5() throws Exception {
        List<List<Object>> param = new ArrayList<>();
        List<Object> aa = new ArrayList<>();
        aa.add(1);
        aa.add("aaa");
        aa.add("bb");
        param.add(aa);
        aa = new ArrayList<>();
        aa.add(2);
        aa.add("cc");
        aa.add("dd");
        param.add(aa);
        List<List<String>> heads = new ArrayList<>();

        List<String> head = new ArrayList<>();
        head.add("你是谁");
        heads.add(head);
        head = new ArrayList<>();
        head.add("你在哪");
        heads.add(head);
        head = new ArrayList<>();
        head.add("你好呀");
        heads.add(head);
        List<MetaExcelWriteDTO> dataList = new ArrayList<>();
        MetaExcelWriteDTO data = new MetaExcelWriteDTO().setHeads(heads).setMapList(param);
        dataList.add(data);
        data = new MetaExcelWriteDTO().setHeads(heads).setMapList(param);
        dataList.add(data);
        String filePath = "D:\\Temp\\你好呀" + System.currentTimeMillis() + ".xlsx";
        OutputStream os = Files.newOutputStream(Paths.get(filePath));
        operate.writeByTemplate(dataList, getMapTempInputStream(), os, true);
        System.out.println("文件写出成功! ");
    }

    /**
     * 测试 Map数据写出文件流-单个 sheet数据写出
     * 参数: 入参对象集合 List<MetaExcelWriteDTO>, 输出文件流OutputStream
     */
    @Test
    public void testWriteByTemplate6() throws Exception {
        List<List<Object>> param = new ArrayList<>();
        List<Object> aa = new ArrayList<>();
        aa.add(1);
        aa.add("aa");
        aa.add(new Date());
        param.add(aa);
        aa = new ArrayList<>();
        aa.add(2);
        aa.add("cc");
        aa.add("2022-12-31 00:12:13");
        param.add(aa);
        List<List<String>> heads = new ArrayList<>();
        List<String> head = new ArrayList<>();
        head.add("你是谁");
        heads.add(head);
        head = new ArrayList<>();
        head.add("你在哪");

        heads.add(head);
        head = new ArrayList<>();
        head.add("你好呀");
        heads.add(head);
        List<MetaExcelWriteDTO> metas = new ArrayList<>();
        MetaExcelWriteDTO data = new MetaExcelWriteDTO().setHeads(heads).setMapList(param).setSheetNo(0);
        metas.add(data);
        String filePath = "D:\\Temp\\你好呀" + System.currentTimeMillis() + ".xlsx";
        OutputStream os = Files.newOutputStream(Paths.get(filePath));
        operate.writeByTemplate(metas, getMapTempInputStream(), os, true);
        filePath = "D:\\Temp\\你好呀" + System.currentTimeMillis() + ".xlsx";
        operate.writeByTemplate(metas, getMapTempInputStream(), filePath, true);
    }

    /**
     * 测试写出文件流-数据写出并填充下拉框
     * 参数: 入参对象集合 List<MetaExcelWriteDTO>, 输出文件流OutputStream
     */
    @Test
    public void testWriteByTemplate7() throws Exception {
        List<PaExampleOneDto> dataList = new ArrayList<>();
        addDataOne(dataList);
        List<MetaExcelWriteDTO> metas = new ArrayList<>();
        MetaExcelWriteDTO meta = new MetaExcelWriteDTO().setParamClazz(PaExampleOneDto.class).setDataList(dataList);
        metas.add(meta);
        String filePath = "D:\\Temp\\你好呀" + System.currentTimeMillis() + ".xlsx";
        OutputStream os = Files.newOutputStream(Paths.get(filePath));
        operate.writeByTemplate(metas, getObjTempInputStream(), os, false);
    }

    public FileInputStream getObjTempInputStream() throws IOException {
        String excelPath = System.getProperty("file.separator") + "template" + System.getProperty("file.separator") + "objTemplate.xlsx";
        FileInputStream ins = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return ins;
    }

    public FileInputStream getMapTempInputStream() throws IOException {
        String excelPath = System.getProperty("file.separator") + "template" + System.getProperty("file.separator") + "mapTemplate.xlsx";
        FileInputStream ins = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return ins;
    }

    public void addDataOne(List<PaExampleOneDto> dataList) {
        PaExampleOneDto dto = new PaExampleOneDto();
        dto.setDocLevel("11");
        dto.setDocName("22");
        dto.setPlanDesc("aa");
        dto.setPlanType("bb");
        dto.setPlanStartTime(new Date());
        dto.setPlanEndTime(new Date());
        dto.setUm("cc");
        dataList.add(dto);
    }

    public void addDataTwo(List<PaExampleTwoDto> data2List) {
        PaExampleTwoDto dto1 = new PaExampleTwoDto();
        dto1.setDocLevel1("11");
        dto1.setDocName1("22");
        dto1.setPlanDesc1("aa");
        dto1.setPlanType1("bb");
        dto1.setPlanStartTime1(new Date());
        dto1.setPlanEndTime1(new Date());
        dto1.setUm1("cc");
        data2List.add(dto1);
    }
}